﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.Text.RegularExpressions;

public partial class shiyan : System.Web.UI.Page
{
    DateTime datm;
    protected void Page_Load(object sender, EventArgs e)
    {

        TextBox3.ReadOnly = true;
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
        string JudgePower = Session["power"].ToString();
        if (JudgePower != "超级管理员" && JudgePower != "销售" && JudgePower != "主管")
        {

            Response.Redirect("~/PowerError.aspx?");
        }
        if (ViewState["Count"] != null)
        {
            for (int i = 0; i < Convert.ToInt16(ViewState["Count"]); i++)
                AddTextBoxs();
            AddButton();
        }
        else
        {
            Button1_Click(sender, e);
        }
        if (!IsPostBack)
        {
            this.bind();
        }
        //  times = datm.ToString("yyyy-MM-dd HH:mm");
    }

    public void Button1_Click(object sender, System.EventArgs e)
    {
        AddTextBoxs();
        if (ViewState["Count"] == null) AddButton();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
    }
    private void AddTextBoxs()
    {
        TableRow tr = new TableRow();
        TableCell tc1 = new TableCell();
        TextBox t = new TextBox();
        t.ReadOnly = true;
        t.ID = "Name" + Table1.Rows.Count;
        tc1.Controls.Add(t);

        TableCell tc2 = new TableCell();
        DropDownList dpl = new DropDownList();
        dpl.ID = "dpl" + Table1.Rows.Count;
        for (int i = 1; i < 4; i++)
            dpl.Items.Add(i.ToString());
        tc2.Controls.Add(dpl);

        TableCell tc3 = new TableCell();
        Label bianhao = new Label();
        bianhao.ID = "bianhao" + Table1.Rows.Count;
        bianhao.Text = Table1.Rows.Count.ToString();
        tc3.Controls.Add(bianhao);

        TableCell tc4 = new TableCell();
        TextBox sum = new TextBox();
        sum.ID = "shuliang" + Table1.Rows.Count;
        tc4.Controls.Add(sum);

        TableCell tc5 = new TableCell();
        TextBox name = new TextBox();
        name.ReadOnly = true;
        name.ID = "mingzi" + Table1.Rows.Count;
        tc5.Controls.Add(name);

        TableCell tc6 = new TableCell();
        TextBox type = new TextBox();
        type.ReadOnly = true;
        type.ID = "leixing" + Table1.Rows.Count;
        tc6.Controls.Add(type);

        TableCell tc7 = new TableCell();
        TextBox brand = new TextBox();
        brand.ReadOnly = true;
        brand.ID = "paizi" + Table1.Rows.Count;
        tc7.Controls.Add(brand);

        tr.Cells.Add(tc3);
        tr.Cells.Add(tc1);
        tr.Cells.Add(tc2);
        tr.Cells.Add(tc4);
        tr.Cells.Add(tc5);
        tr.Cells.Add(tc6);
        tr.Cells.Add(tc7);
        Table1.Rows.Add(tr);

    }
    private void sameWork(object sender, EventArgs e)
    {
        //Table1.Rows.RemoveAt(1);
        //HttpContext.Current.Response.Write("<script language='javascript'>alert('dada');</script>");
       // Table1.Rows.Remove(this
    }  
    private void AddButton()
    {
        //Button b = new Button();
        //b.ID = "btn";
        //b.Text = "添加";
        //b.Click += new System.EventHandler(btn_Click);
        //PlaceHolder1.Controls.Add(b);
    }
    private void btn_Click(object sender, System.EventArgs e)
    {

        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "select * from Goods where ('('+GBrand+')'+GCHName+'('+GXinhao+')') like '%" + ListBox1.SelectedValue.ToString() + "%'";
        SqlCommand com = new SqlCommand(sql, myConn);
        com.ExecuteNonQuery();
        SqlDataReader read = com.ExecuteReader();
        read.Read();
        string GID = read["GID"].ToString();
        read.Close();
        string sqlll = "select GXinhao from Goods where GID='" + GID + "'";
        SqlCommand comm = new SqlCommand(sqlll, myConn);
        comm.ExecuteNonQuery();
        SqlDataReader readd = comm.ExecuteReader();
        readd.Read();
        string GX = readd["GXinhao"].ToString();
        readd.Close();
        myConn.Close();
        ((TextBox)Table1.Rows[Table1.Rows.Count - 1].FindControl("Name" + (Table1.Rows.Count - 1))).Text = GX;
        // DropDownList1.SelectedValue = GX;
    }

    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select GID,('('+GBrand+')'+GCHName+'('+GXinhao+')') AS DisplayName from Goods where  GCHName like '%" + TextBox1.Text + "%' or GXinhao like '%" + TextBox1.Text + "%' ";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        ListBox1.DataSource = myDs;
        ListBox1.DataTextField = "DisplayName";
        ListBox1.DataBind();
        myDa.Dispose();
        myDs.Dispose();
        myConn.Close();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Table1.Rows.Count; i++)
        {
            Response.Write(((DropDownList)Table1.Rows[i].FindControl("dp2" + i)).SelectedValue + "<br>");
        }

    }

    public void bind()
    {

        TextBox3.Text = Session["id"].ToString();
        SqlConnection myConn = GetConnection();
        myConn.Open();


        string sql = "select (bianhao+PName) as PAName  from Projects order by bianhao desc";
        SqlDataAdapter sqlda = new SqlDataAdapter(sql, myConn);
        DataSet sqlds = new DataSet();
        sqlda.Fill(sqlds);
        DropDownList1.DataSource = sqlds;
        DropDownList1.DataTextField = "PAName";
        DropDownList1.DataBind();
        sqlda.Dispose();
        sqlds.Dispose();

        this.DropDownList1.Items.Insert(0, new ListItem("请选择项目", "请选择项目"));
        myConn.Close();
    }

    public bool IsNumberic(string str)
    {

        Regex reg = new Regex("^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$");
        Match ma = reg.Match(str);
        if (ma.Success)
        {
            //是数字  
            return true;
        }
        else
        {
            //不是数字    
            return false;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "请选择项目")
        {
            Response.Write("<script>alert('请选择项目!')</script>");
            return;
        }
        int isnumber = 0;
        datm = DateTime.Now;
        if (Table1.Rows.Count != 0 && ((TextBox)Table1.Rows[0].FindControl("shuliang" + 0)).Text!=""&& TextBox2.Text!="")
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();

            string sqll = "select PID from Projects where (bianhao+PName)='" + DropDownList1.SelectedValue + "'";
            SqlCommand sqlcmd = new SqlCommand(sqll, myConn);
            sqlcmd.ExecuteNonQuery();
            SqlDataReader readpid = sqlcmd.ExecuteReader();
            readpid.Read();
            string PID = readpid["PID"].ToString();
            readpid.Close();

            


            for (int i = 0; i < Table1.Rows.Count; i++)
            {
                if (IsNumberic(((TextBox)Table1.Rows[i].FindControl("shuliang" + i)).Text))
                {
                    isnumber = isnumber + 1;
                }
            }

            if (isnumber == Table1.Rows.Count)
            {
                string sql = "insert into dan (DName,DPid,DDes,DXiao,DHetong,beizhu) values ('" + datm.ToString("yyyy-MM-dd HH:mm") + TextBox3.Text.Trim().ToString() + "','" + PID + "','购货单','" + TextBox3.Text.Trim().ToString() + "','" + TextBox2.Text.Trim().ToString() + "','"+  TextBox4.Text.Trim().ToString() +"')";
                SqlCommand cmd = new SqlCommand(sql, myConn);
                cmd.ExecuteNonQuery();


                string sql1 = "select id from Dan where DName='" + datm.ToString("yyyy-MM-dd HH:mm") + TextBox3.Text.Trim().ToString() + "'";
                SqlCommand cmd1 = new SqlCommand(sql1, myConn);
                cmd1.ExecuteNonQuery();
                SqlDataReader readid = cmd1.ExecuteReader();
                readid.Read();
                string id = readid["id"].ToString();
                readid.Close();
                for (int i = 0; i < Table1.Rows.Count; i++)
                {
                    if (((TextBox)Table1.Rows[i].FindControl("Name" + i)).Text != "")
                    {
                        ruku(((TextBox)Table1.Rows[i].FindControl("Name" + i)).Text, Convert.ToInt32(((DropDownList)Table1.Rows[i].FindControl("dpl" + i)).SelectedValue), TextBox3.Text.Trim().ToString(), ((TextBox)Table1.Rows[i].FindControl("shuliang" + i)).Text);
                    }
                }

                myConn.Close();
                Response.Redirect("~/RecordsList.aspx?");
            }
            else
            {
                Response.Write("<script>alert('输入的数字有误!')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('订单中信息未完整!')</script>");
        }
    }


    protected void ruku(string xinhao, int cangku, string name, string sum)
    {
        //  DateTime dt = DateTime.Now;
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "select * from Goods where GXinhao='" + xinhao + "'";
        SqlCommand comm = new SqlCommand(sql, myConn);
        comm.ExecuteNonQuery();
        SqlDataReader read = comm.ExecuteReader();
        read.Read();
        string GID = read["GID"].ToString();
        read.Close();

        string sqlpro = "select * from Projects where (bianhao+PName)='" + DropDownList1.SelectedValue + "'";
        SqlCommand mycom = new SqlCommand(sqlpro, myConn);
        mycom.ExecuteNonQuery();
        SqlDataReader readpro = mycom.ExecuteReader();
        readpro.Read();
        string PID = readpro["PID"].ToString();
        readpro.Close();

        string logname;
        string sqllname = "select UID from Users where UName='" + Session["name"].ToString() + "'";
        SqlCommand comname = new SqlCommand(sqllname, myConn);
        comname.ExecuteNonQuery();
        SqlDataReader readname = comname.ExecuteReader();
        readname.Read();
        string UID = readname["UID"].ToString();
        readname.Close();
        logname = UID;

        string sql1 = "select id from Dan where DName='" + datm.ToString("yyyy-MM-dd HH:mm") + TextBox3.Text.Trim().ToString() + "'";
        SqlCommand cmd1 = new SqlCommand(sql1, myConn);
        cmd1.ExecuteNonQuery();
        SqlDataReader readid = cmd1.ExecuteReader();
        readid.Read();
        string id = readid["id"].ToString();
        readid.Close();

        DateTime dt = DateTime.Now;
        bool type = true;
        string sqll;

        string sqlll;

        string sqllll = "select * from Store where SGID='" + GID + "'and SWID='" + cangku + "'";
        SqlCommand com1 = new SqlCommand(sqllll, myConn);
        SqlDataReader dr = com1.ExecuteReader();
        if (name != "" && DropDownList1.SelectedValue != "" && sum != "")
        {
            if (dr.Read())
            {
                dr.Close();
                sqll = "insert into RecordsIN (RGID,RWID,RQuantity,RType,RManager,RHandler,RDate,RifAOG,RPID,RDID) values('" + GID + "','" + cangku + "','" + Convert.ToDouble(sum) + "','" + type + "','" + logname + "','" + TextBox3.Text.Trim().ToString() + "','" + dt + "','0','" + PID + "','" + id + "')";
                SqlCommand com = new SqlCommand(sqll, myConn);
                com.ExecuteNonQuery();
                myConn.Close();
                Response.Write("<script>alert('下单成功!')</script>");
            }
            else
            {
                dr.Close();
                sqlll = "insert into Store (SGID,SWID,STopLine,SbaseLine,SQuantity) values('" + GID + "','" + cangku + "','500','100','0')";
                sqll = "insert into RecordsIN (RGID,RWID,RQuantity,RType,RManager,RHandler,RDate,RifAOG,RPID,RDID) values('" + GID + "','" + cangku + "','" + Convert.ToDouble(sum) + "','" + type + "','" + logname + "','" + TextBox3.Text.Trim().ToString() + "','" + dt + "','0','" + PID + "','" + id + "')";
                SqlCommand com = new SqlCommand(sqll, myConn);
                com.ExecuteNonQuery();
                com.CommandText = sqlll;
                com.ExecuteNonQuery();
                myConn.Close();
                Response.Write("<script>alert('下单成功!')</script>");
            }
        }
        else
            Response.Write("<script>alert('录入信息不符合要求!')</script>");
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "请选择项目")
        {
            Response.Write("<script>alert('请选择项目!')</script>");
            return;
        }
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "select * from Goods where ('('+GBrand+')'+GCHName+'('+GXinhao+')') like '%" + ListBox1.SelectedValue.ToString() + "%'";
        SqlCommand com = new SqlCommand(sql, myConn);
        com.ExecuteNonQuery();
        SqlDataReader read = com.ExecuteReader();
        read.Read();
        string GID = read["GID"].ToString();
        read.Close();
        string sqlll = "select * from Goods where GID='" + GID + "'";
        SqlCommand comm = new SqlCommand(sqlll, myConn);
        comm.ExecuteNonQuery();
        SqlDataReader readd = comm.ExecuteReader();
        readd.Read();
        string GX = readd["GXinhao"].ToString();
        string NAME = readd["GCHName"].ToString();
        string TYPE = readd["GTYPEName"].ToString();
        string BRAND = readd["GBrand"].ToString();
        readd.Close();
        myConn.Close();
        ((TextBox)Table1.Rows[Table1.Rows.Count - 1].FindControl("Name" + (Table1.Rows.Count - 1))).Text = GX;
        ((TextBox)Table1.Rows[Table1.Rows.Count - 1].FindControl("mingzi" + (Table1.Rows.Count - 1))).Text = NAME;
        ((TextBox)Table1.Rows[Table1.Rows.Count - 1].FindControl("leixing" + (Table1.Rows.Count - 1))).Text = TYPE;
        ((TextBox)Table1.Rows[Table1.Rows.Count - 1].FindControl("paizi" + (Table1.Rows.Count - 1))).Text = BRAND;
        // DropDownList1.SelectedValue = GX;
    }
}